/*
 * InputDir.cpp
 *
 *  Created on: Jul 23, 2013
 *      Author: Dan
 */

#include "InputDir.h"

namespace sm {

InputDir::InputDir(std::string aDirName) {
	DIR* dp = opendir(aDirName.c_str());
	if (dp == NULL) {
		Log::error("Problem opening the Input Directory");
	} else {
		theDirName = aDirName;
	}
	closedir(dp);
}

std::string InputDir::getFileName() {
	bool found = false;
	DIR *dp;
	std::string filepath;
	struct dirent *dirp;
	struct stat filestat;

	dp = opendir(theDirName.c_str());
	if (dp == NULL) {
		Log::error("Problem opening the Input Directory");
		return "";
	}
	while ((dirp = readdir(dp)) && !found) {
		filepath = theDirName + "/" + dirp->d_name;
		if (stat(filepath.c_str(), &filestat))
			continue;
		if (S_ISDIR( filestat.st_mode ))
			continue;
		found = true;
	}
	closedir(dp);
	if (!found) {
		filepath = "";
	}
	return filepath;
}

InputDir::~InputDir() {
	// TODO Auto-generated destructor stub
}

} /* namespace monkeyXML */
